<?php
/**
 * Created by PhpStorm.
 * User: ADMIN
 * Date: 2021/6/27
 * Time: 7:07
 */
namespace app\adminapi\validate;

use app\common\model\ChangeCategory;

class ChangeCategoryAdd extends Base
{
    protected $rule = [
        'name' => 'require|nameUnique',
        'status' => 'require|checkStatus',
    ];

    protected $message = [
        'name.nameUnique' => '变更类别名唯一',

        'status.checkStatus' => 'status值只能为0或1',
    ];

    /**
     * 变更类别名唯一
     * @param $value
     * @return bool
     * @throws \think\Exception
     */
    protected function nameUnique($value)
    {
        if(db('change_category')->where("name='{$value}'")->count()) return false;
        return true;
    }

    /**
     * 状态验证
     * @param $value
     * @return bool
     */
    protected function checkStatus($value)
    {
        if(!ChangeCategory::getStatusText($value)) return false;
        return true;
    }

}